//
//  BMHAreaPickView.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/4/17.
//  Copyright © 2017年 Maoyx. All rights reserved.
//

#import "BMHAreaPickView.h"
#import <UtilsMacro.h>
#import <UIView+Layout.h>
#import "BMHDBManager.h"
#import "BMHDBManager+Area.h"
#import "BMHAreaModel.h"
#import <ReactiveCocoa.h>

@interface BMHAreaPickView ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSMutableArray *provinceArray;
@property (nonatomic, strong) NSMutableArray *cityArray;
@property (nonatomic, strong) NSMutableArray *areaArray;
@property (nonatomic, unsafe_unretained) NSInteger provinceRow;
@property (nonatomic, unsafe_unretained) NSInteger cityRow;
@property (nonatomic, unsafe_unretained) NSInteger countyRow;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *cancleBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, assign) NSInteger areaId;
@end

@implementation BMHAreaPickView

-(RACSubject *)dismissSignal{
    if (_dismissSignal == nil) {
        _dismissSignal = [[RACSubject alloc]init];
    }
    return _dismissSignal;
}

-(RACSubject *)sureSignal{
    if (_sureSignal == nil) {
        _sureSignal = [[RACSubject alloc]init];
    }
    return _sureSignal;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0);
        
        self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height, self.width, 250)];
        self.contentView.backgroundColor = RGB(237, 237, 237);
        [self addSubview:self.contentView];
        
        UIView *optionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.width, 44)];
        optionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:optionView];
        
        UIButton *cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 88, optionView.height)];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:RGB(111, 157, 245) forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        @weakify(self);
        [[cancleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self dismiss];
        }];
        [optionView addSubview:cancleBtn];
        
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(optionView.width - cancleBtn.width, 0, cancleBtn.width, cancleBtn.height)];
        [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [sureBtn setTitleColor:RGB(111, 157, 245) forState:UIControlStateNormal];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            NSString *address = [self getAddressWithAreaId:self.areaId == 0 ? 500 : self.areaId];
            [self.sureSignal sendNext:address];
            [self dismiss];
        }];
        [optionView addSubview:sureBtn];
        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, (self.contentView.height - 150 + 44) / 2, self.contentView.width, 150)];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.pickerView];
        
        self.provinceRow = 0;
        self.cityRow = 0;
        self.countyRow = 0;
        
        self.provinceArray = [[[BMHDBManager sharedManager] queryAddressWithParentId:1] mutableCopy];
        BMHAreaModel *provinceModel = [self.provinceArray firstObject];
        self.cityArray = [[[BMHDBManager sharedManager] queryAddressWithParentId:provinceModel.areaId] mutableCopy];
        BMHAreaModel *cityModel = [self.cityArray firstObject];
        self.areaArray = [[[BMHDBManager sharedManager] queryAddressWithParentId:cityModel.areaId] mutableCopy];;
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.provinceArray.count;
    } else if (component == 1) {
        return self.cityArray.count;
    } else {
        return self.areaArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    BMHAreaModel *model = nil;
    if (component == 0) {
        model = (BMHAreaModel *)self.provinceArray[row];
    } else if (component == 1) {
        model =  self.cityArray[row];
    } else {
        model =  self.areaArray[row];
    }
    label.text = model.areaName;
    return label.text;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    BMHAreaModel *provinceModel = nil;
    BMHAreaModel *cityModel = nil;
    BMHAreaModel *areaModel = nil;
    if (component == 0) {//第一行改变
        provinceModel = self.provinceArray[row];
        [self.cityArray removeAllObjects];
        [self.cityArray addObjectsFromArray:[[BMHDBManager sharedManager] queryAddressWithParentId:provinceModel.areaId]];
        cityModel = self.cityArray[0];
        [self.areaArray removeAllObjects];
        [self.areaArray addObjectsFromArray:[[BMHDBManager sharedManager] queryAddressWithParentId:cityModel.areaId]];
        self.provinceRow = row;
        self.cityRow = 0;
        self.countyRow = 0;
        //刷新列
        [self.pickerView reloadComponent:1];
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
    } else if (component == 1) {//第二行改变
        cityModel = self.cityArray[row];
        [self.areaArray removeAllObjects];
        [self.areaArray addObjectsFromArray:[[BMHDBManager sharedManager] queryAddressWithParentId:cityModel.areaId]];
        self.cityRow = row;
        self.countyRow = 0;
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
    } else {//第三行改变
        self.countyRow = row;
    }
    provinceModel = [self.provinceArray objectAtIndex:self.provinceRow];
    cityModel = [self.cityArray objectAtIndex:self.cityRow];
    areaModel = [self.areaArray objectAtIndex:self.countyRow];
    self.areaId = areaModel.areaId;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    if (point.y < self.contentView.top) {
        [self dismiss];
    }
}

- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = RGBA(0, 0, 0, 0.5);
        self.contentView.top = self.height - self.contentView.height;
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = RGBA(0, 0, 0, 0);
        self.contentView.top = self.height;
    } completion:^(BOOL finished) {
        if (finished) {
            [self.dismissSignal sendNext:nil];
        }
    }];
}

- (NSString *)getAddressWithAreaId:(NSInteger)areaId {
    BMHAreaModel *areaModel = [[BMHDBManager sharedManager] queryAddressWithAreaId:areaId];
    BMHAreaModel *cityModel = [[BMHDBManager sharedManager] queryAddressWithAreaId:areaModel.parentId];
    BMHAreaModel *provinceModel = [[BMHDBManager sharedManager] queryAddressWithAreaId:cityModel.parentId];
    return F(@"%@ %@ %@",provinceModel.areaName,cityModel.areaName,areaModel.areaName);
}
@end
